﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Indie_Game_Contest
{
    class Enemy : CBaseObject
    {
        //////////////////////////////////////////////////////////////////////////
        // Data Members
        AIState state = new AIState();
        Vector2 velocity = Vector2.Zero;
        bool Attack;

        public bool Attack1
        {
            get { return Attack; }
            set { Attack = value; }
        }

  
        bool jumped = false;
        Vector2 prevPos = Vector2.Zero;
        float health = 100;

        public float Health1
        {
            get { return health; }
            set { health = value; }
        }

        //////////////////////////////////////////////////////////////////////////
        // Methods
        public Enemy()
        {
            state.Enemy = this;
        }
        public override void LoadContent(Rectangle sourceRect, Vector2 vPos, Color mask, Texture2D tImage, float fRotation, float fScale, int nWidth, int nHeight, int nID)
        {
            SrcRect = sourceRect;
            Position = vPos;
            ColorMask = mask;
            Image = tImage;
            Rotation = fRotation;
            Scale = fScale;
            Width = nWidth;
            Height = nHeight;
            center = new Vector2(width / 2, height / 2);
            TextureID = nID;
            type = ObjectType.Enemy;

            Collision = true;
        }
        public override void Render()
        {
            WorldData.spriteBatch.Draw(TextureManager.Instance.GetTexture(textureID),
                                        position, null,
                                        colorMask, 0, center, 1, SpriteEffects.None, 0);
        }
        public override void Update(float dt)
        {
            prevPos = position;

            state.Update(dt, this);

            velocity.Y += 70;

            if (velocity.Y >= 750)
                velocity.Y = 750;

            position += velocity * dt;
        }

        public override CollisionSide CheckCollision(CBaseObject obj)
        {
            CollisionSide side = base.CheckCollision(obj);

            if (obj.Type == ObjectType.Tile && side != CollisionSide.None)
            {
                switch (side)
                {
                    case CollisionSide.Bottom:
                        {
                            jumped = false;
                            position.Y = obj.Position.Y - (center.Y + obj.Center.Y * obj.Scale);
                            break;
                        }
                    case CollisionSide.Top:
                        {
                            //velocity.Y = 0;
                            position.Y = prevPos.Y;
                            break;
                        }
                    case CollisionSide.Right:
                        //position.X = obj.Position.X - width*obj.Scale;
                        break;
                    case CollisionSide.Left:
                        //position.X = obj.Position.X + obj.Width;//*obj.Scale;
                        break;
                }
            }

            return side;
        }

        public bool Take_Damage(float dmg)
        {
            if ((health -= dmg) <= 0)
            {
                MessageSystem.Instance.SendMessage(new EnemyDeadMessage(this));
            }
            return false;
        }

        //////////////////////////////////////////////////////////////////////////
        // Properties
        protected Indie_Game_Contest.AIState State
        {
            get { return state; }
            set { state = value; }
        }
        public float Health
        {
            get { return health; }
            set { health = value; }
        }
        public bool Jumped
        {
            get { return jumped; }
            set { jumped = value; }
        }
        public Microsoft.Xna.Framework.Vector2 Velocity
        {
            get { return velocity; }
            set { velocity = value; }
        }
    }
}
